package com.example.parkingproject.controller;

import com.example.parkingproject.code.ServiceCode;
import com.example.parkingproject.config.Sms;
import com.example.parkingproject.ex.ServiceException;
import com.example.parkingproject.jopo.dto.UserLoginDto;
import com.example.parkingproject.jopo.dto.UserRegDto;
import com.example.parkingproject.jopo.dto.UserUpdateDto;
import com.example.parkingproject.jopo.entity.Password;
import com.example.parkingproject.jopo.vo.StopHistoryVo;
import com.example.parkingproject.jopo.vo.UserLoginVo;
import com.example.parkingproject.jopo.vo.getUserVo;
import com.example.parkingproject.result.JsonResult;
import com.example.parkingproject.jopo.vo.spaceOccupancy;
import com.example.parkingproject.service.UserService;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("login")
    public JsonResult<UserLoginVo> login(@RequestBody UserLoginDto userLoginDto) {
        UserLoginVo userLoginVo = userService.login(userLoginDto);
        log.debug("获取的参数{}", userLoginVo);
        return JsonResult.ok(userLoginVo);
    }

    @RequestMapping("reg")
    public JsonResult reg(@RequestBody UserRegDto userRegDto) {
        log.debug("前端参数{}", userRegDto);
        userService.reg(userRegDto);
        return JsonResult.ok();
    }

    @RequestMapping("/getUserByCardId")
    public JsonResult<getUserVo> getUserByCardId(String cardId) {
        getUserVo getUserVo = userService.getUserByCardId(cardId);
        log.debug("查询处理的参数{}", getUserVo);
        return JsonResult.ok(getUserVo);
    }

    @RequestMapping("/update")
    public JsonResult update(@RequestBody UserUpdateDto userUpdateDto) {
        userService.update(userUpdateDto);
        return JsonResult.ok();
    }

    @RequestMapping("/spaceOccupancy")
    public JsonResult<List<spaceOccupancy>> spaceOccupancy(String carId) {
        List<spaceOccupancy> list = userService.getSpaceOccupancy(carId);
        log.debug("查询参数为{}", list);
        return JsonResult.ok(list);
    }

    @RequestMapping("/getHistory")
    public JsonResult<List<StopHistoryVo>> getHistory(String cardId) {

        List<StopHistoryVo> list = userService.getHistory(cardId);
        log.debug("获取参数为{}", list);
        return JsonResult.ok(list);
    }

    @RequestMapping("/search")
    public JsonResult<List<StopHistoryVo>> search(@RequestBody StopHistoryVo stopHistoryVo) {
        List<StopHistoryVo> list = userService.search(stopHistoryVo);
        return JsonResult.ok(list);
    }

    @RequestMapping("/getUserNumber")
    public JsonResult<Integer> getUserNumber() {
        Integer count = userService.getUserNumber();
        log.debug("查询出参数{}", count);
        return JsonResult.ok(count);
    }

    @RequestMapping("/openVip")
    public JsonResult openVip(String cardId) {
        log.debug("传入参数{}", cardId);
        userService.openVip(cardId);
        return JsonResult.ok();
    }

    @RequestMapping("/updatePassword")
    public JsonResult updatePassword(@RequestBody Password password) {
        log.debug("传入参数{}", password);
        userService.updatePassword(password);
        return JsonResult.ok();
    }


    // 使用Jedis连接Redis
    Jedis jedis = new Jedis("127.0.0.1", 8080);

    // 使用Lettuce连接Redis
    RedisClient redisClient = RedisClient.create("redis://localhost");
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();

    @RequestMapping("getSms")
    public JsonResult getSms(@Param("phone") String phone) {
        Map<String, Object> codeMap = new HashMap<>();
        Random random = new Random();
        String code = String.valueOf(random.nextInt(900000) + 100000);
        log.debug("验证码：{}", code);
        codeMap.put("code", code);
        Boolean result = Sms.sendMessage(phone, codeMap);

        int timeout = 300;
        commands.setex("code", timeout, code);
        if (result == false) {
            throw new ServiceException(ServiceCode.error_bad_request, "发送失败请检查后重试");
        }
        return JsonResult.ok();
    }


    @RequestMapping("/updatePasswordByPhone")
    public JsonResult updatePasswordByPhone(@RequestBody Password password) {
        log.debug("传入的password参数{}",password);
        String code = commands.get("code");
        log.debug("获取的数据{}", code);
        userService.updatePasswordByPhone(password, code);

        return JsonResult.ok();
    }


}
